//
//  MZYsecViewController.m
//  WristWatch
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MZYsecViewController.h"
#import "MZYsecTableViewCell.h"
#import "MZYHeader.h"
#import "MZYListModel.h"
#import "MZYpicModel.h"
#import "MZYthiViewController.h"
@interface MZYsecViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *titleTableView;
@property(nonatomic, retain)UIImageView *titleImageView;
@property(nonatomic, retain)UIView *titleView;
@property(nonatomic, retain)NSMutableArray *secArr;
@property (nonatomic, retain) NSMutableArray *oneArr;
@property (nonatomic, retain) NSMutableArray *gallary;
@property (nonatomic, retain) NSMutableArray *picArr;


@end

@implementation MZYsecViewController

- (void)dealloc {
    [_titleTableView release];
    [_titleImageView release];
    [_titleView release];
    [_secArr release];
    [_oneArr release];
    [_gallary release];
    [_picArr release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createData];
    [self creatrView];
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   if (self) {
        self.oneArr = [NSMutableArray array];
       self.gallary = [NSMutableArray array];
       self.picArr = [NSMutableArray array];
    }
    return self;
}

- (void)creatrView {
    self.titleTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.titleTableView];
    [_titleTableView release];
    self.titleTableView.dataSource = self;
    self.titleTableView.delegate = self;
    self.navigationController.navigationBar.translucent = NO;
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 5 * 3 - 17)];
    //    self.titleView.backgroundColor = [UIColor redColor];
    [self.titleTableView addSubview:self.titleView];
    self.titleTableView.tableHeaderView = self.titleView;
    [_titleView release];
    
    
    for (NSInteger i = 0; i < self.gallary.count; i ++) {
    MZYpicModel *model = self.picArr[0];
    self.titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT / 2)];
    [self.titleView addSubview:self.titleImageView];
//    self.titleTableView.tableHeaderView = self.titleImageView;
    [self.titleImageView sd_setImageWithURL:[NSURL URLWithString:model.gallary[i]] placeholderImage:nil];
    [self.titleImageView release];
    }
    
    
//    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT / 2, WIDTH, HEIGHT / 10 - 17)];
//    [self.titleView addSubview:titleLabel];
//    titleLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
//    titleLabel.text = @"   参数";
//    titleLabel.font = [UIFont boldSystemFontOfSize:20];
//    [titleLabel release];
    
//    UILabel *titleLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT / 2 + 42, WIDTH, HEIGHT / 35)];
//    [self.titleView addSubview:titleLabel1];
////    titleLabel1.backgroundColor = [UIColor greenColor];
//    titleLabel1.text = @"基本信息";
//    titleLabel1.textColor = [UIColor grayColor];
//    titleLabel1.font = [UIFont boldSystemFontOfSize:13];
//    [titleLabel1 release];
    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    button.frame = CGRectMake(WIDTH * 4 / 5, HEIGHT / 2, WIDTH / 5, HEIGHT / 10 - 17);
////    button.backgroundColor = [UIColor redColor];
//    [button setTitle:@"更多参数" forState:UIControlStateNormal];
//    button.titleLabel.font = [UIFont systemFontOfSize:13];
////    button.titleLabel.textAlignment = 2;
//    [self.titleView addSubview:button];
//    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}

- (void)createData {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"http://h5.ttime.com/ajax/mall/product/%@?token=92c06c9f918d3185b4b78110996bcd2b&cityid=2&v=2.2.6&from=info_all&flag=app", self.product_id];
    
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = (NSMutableDictionary *)responseObject;
        self.secArr = [NSMutableArray array];
        
        MZYPictureModel *pic = [[MZYPictureModel alloc] init];
        NSArray *mArr = dic[@"result"][@"mkt_prices"];
        for (NSDictionary *tempDic in mArr) {
            [pic setValuesForKeysWithDictionary:tempDic];
            [self.secArr addObject:pic];
        }
         [self.titleImageView sd_setImageWithURL:[NSURL URLWithString:pic.gallary[0]] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
        [pic release];
        
        NSArray *parameterArr = dic[@"result"][@"parameter"];
        NSDictionary *dic0 = parameterArr[0];
        NSArray *listArr = dic0[@"list"];
        for (NSDictionary *dicList in listArr) {
            MZYListModel *model = [[MZYListModel alloc] init];

            [model setValuesForKeysWithDictionary:dicList];
            [self.oneArr addObject:model];
            [model release];
        }
        NSDictionary *resultDic = dic[@"result"];
        self.gallary = resultDic[@"gallary"];
        MZYpicModel *model = [[MZYpicModel alloc] init];
        [model setValuesForKeysWithDictionary:resultDic];
        [self.picArr addObject:model];
        [model release];
        
        
        
        if (self.secArr.count > 0) {
            [self.titleTableView reloadData];
            [self creatrView];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.oneArr.count;
   
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    MZYsecTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
            cell = [[[MZYsecTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
        }
    MZYListModel *model = self.oneArr[indexPath.row];
    
    cell.priceLabel.text = [NSString stringWithFormat:@"%@:   %@",model.key, model.value];
        return cell;
}

- (void)buttonAction:(UIButton *)button {
    NSLog(@"不要点我了");
    MZYthiViewController *thiVC = [[MZYthiViewController alloc] init];
    [self.navigationController pushViewController:thiVC animated:YES];
    [thiVC release];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

return @"基本信息";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
